package com.wyh.service.serviceImpl.gfiveImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.common.result.Result;
import com.wyh.common.utils.InvoicerecordsConstant;
import com.wyh.common.utils.StringUtils;
import com.wyh.domain.dto.gfive.InvoicerecordsNoDTO;
import com.wyh.domain.dto.gfive.InvoicerecordsPartDTO;
import com.wyh.domain.po.gfive.Invoicerecords;
import com.wyh.domain.po.gfive.Invoicetype;
import com.wyh.domain.vo.gfive.InvoicerecordsIOtherDate;
import com.wyh.domain.vo.gfive.InvoicerecordsNoVO;
import com.wyh.domain.vo.gfive.InvoicerecordsSelectNoVo;
import com.wyh.domain.vo.gfive.PushInvcPartVO;
import com.wyh.mapper.gfivemapper.InvoicerecordsDTOMapper;
import com.wyh.mapper.gfivemapper.InvoicerecordsVOMapper;
import com.wyh.service.gfive.InvoicerecordsDTOService;
import com.wyh.service.gfive.InvoicetypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InvoicerecordsDTOServiceImpl extends ServiceImpl<InvoicerecordsDTOMapper, Invoicerecords>
        implements InvoicerecordsDTOService {

    @Resource
    private InvoicerecordsDTOMapper InvoDTOMapper;
    @Resource
    private InvoicetypeService InvoType;

    @Resource
    private InvoicerecordsVOMapper InMapper;
//    @Resourcees
//    private UploadState uploadState;

    @Override
    public Result addInvoicerecords(Integer invoType, String salesside, Double amount, Integer type, Integer billid) {
        //先通过账单id查询出需要的数据
        InvoicerecordsPartDTO Part = InvoDTOMapper.slectPart(billid);
        if (ObjectUtils.isEmpty(Part)) {
            return Result.error("数据异常，请联系管理员");
        }
        log.info("Part{}=====", Part);

        String serialnumber = RandomStringUtils.randomNumeric(InvoicerecordsConstant.SERIALNUMBER_LENGTH);
        String noticenumber = RandomStringUtils.randomNumeric(InvoicerecordsConstant.NOTICENUMBER_LENGTH);
        String Invoicecode = RandomStringUtils.randomNumeric(InvoicerecordsConstant.INVOICECODE_LENGTH);
        String Invoicenumber = RandomStringUtils.randomNumeric(InvoicerecordsConstant.INVOICENUMBER_LEGTH);

        Invoicerecords invoicerecords = new Invoicerecords(serialnumber, noticenumber, invoType, null, null, salesside, Part.getOwner(), Part.getBuyer(), null, amount, 1, type, billid);
        int insert = InvoDTOMapper.insert(invoicerecords);
        if (insert != 0) {
            return Result.success("添加成功");
        }
        return Result.error("NO");
    }

    @Override
    public Result moneyBack(String Invoicecode) {

        Invoicerecords invoicerecords = InvoDTOMapper.selectByInvoicecode(Invoicecode);
        log.info("查处来的数据为{}",invoicerecords);
        //获取账单金额 和账单id
        Double amount = invoicerecords.getAmount();
        Integer billid = invoicerecords.getBillid();
        //应交的金额
        Double aDouble = InvoDTOMapper.selecAmount(billid);
        if (aDouble < amount) {
            return Result.error("数据异常，请查看具体数据在进行操作");
        }

        //进行回滚
        int i = InvoDTOMapper.UpdateMoey(amount, billid);
        if (i != 0) {
            return Result.success("回滚成功");
        }
        return Result.error("数据异常");
    }

    //TODO 这里的的上传的发票数量是展示只定义 等完成PDF上传后在 根据PDF上传到数量进行修改
    @Override
    public Result showInvoicerecordsNoPage(Integer pageNum, Integer pageSize) {

        List<InvoicerecordsNoVO> invoicerecordsNoVOS = InvoDTOMapper.showInvoNoPage(pageNum, pageSize);
        //查询 没有处理的票据条数
        QueryWrapper<Invoicerecords> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1).isNotNull("state").and(i->i.eq("type",0));

        Long number = InvoDTOMapper.selectCount(wrapper);
        if (StringUtils.isNotEmpty(invoicerecordsNoVOS)) {
            return Result.success(invoicerecordsNoVOS, Long.valueOf(number));
        }

        return Result.error("数据异常");
    }

    @Override
    public Result selectNoInvoicerecords(InvoicerecordsSelectNoVo InvoSelectNoVO) {

        Integer typeId;
        QueryWrapper<Invoicetype> wrapper = new QueryWrapper<>();
        wrapper.eq("name", InvoSelectNoVO.getInvoicingtype());
        Invoicetype one = InvoType.getOne(wrapper);
        if (one == null) {
            typeId = 0;
        } else {
            typeId = one.getId();
        }
        //查询票据类型
        List<InvoicerecordsNoDTO> invoicerecordsNoVOS = InvoDTOMapper.selecAllNoInvo(typeId, InvoSelectNoVO);
        if (invoicerecordsNoVOS == null || invoicerecordsNoVOS.isEmpty()) {
            return Result.error("查询数据不存在！！");
        }

        ArrayList<InvoicerecordsNoVO> list = new ArrayList<>();
        for (int i = 0; i < invoicerecordsNoVOS.size(); i++) {
            PushInvcPartVO pushInvcPartVO = InMapper.selectPart(
                    invoicerecordsNoVOS.get(i).getInvoicingtype(),
                    invoicerecordsNoVOS.get(i).getOwner(),
                    invoicerecordsNoVOS.get(i).getBuyer(),
                    invoicerecordsNoVOS.get(i).getNoticenumber());

            log.info("查询出来的数据{}", pushInvcPartVO);
            InvoicerecordsNoVO invoicerecordsNoVO = new InvoicerecordsNoVO(
                    invoicerecordsNoVOS.get(i).getNoticenumber(),
                    pushInvcPartVO.getName(),
                    pushInvcPartVO.getCustomername(),
                    pushInvcPartVO.getBuyer(),
                    invoicerecordsNoVOS.get(i).getAmount(),
                    invoicerecordsNoVOS.get(i).getInvoNumber(),
                    invoicerecordsNoVOS.get(i).getInvoicingIsNear(),
                    invoicerecordsNoVOS.get(i).getUninvoicedAmount());
            list.add(invoicerecordsNoVO);
        }

//        log.info("{}",invoicerecordsNoVOS.get(0).getInvoicingtype());
        System.out.println(invoicerecordsNoVOS);
        return Result.success(list);
    }

    @Override
    public Double selectAllMoney(Integer id) {
        double money = InvoDTOMapper.selectAllMoney(id);
        return money;
    }

    @Override
    public Result selectOther(String noticenumber) {
        List<InvoicerecordsIOtherDate> invoicerecordsIOtherDates = InvoDTOMapper.selectOtherAll(noticenumber);
        if(invoicerecordsIOtherDates==null){
            return Result.error("没有上传票据");
        }
        int number=invoicerecordsIOtherDates.size();
        return Result.success(invoicerecordsIOtherDates,Long.valueOf(number));
    }
}
